---
title: LangChainAdapter
description: API Reference for LangChainAdapter.
---

# `LangChainAdapter`

The `LangChainAdapter` module provides a way to transform LangChain output streams into AI streams.
See the [LangChain Adapter documentation](/providers/adapters/langchain) for more information.

## Import

<Snippet text={`import { LangChainAdapter } from "ai"`} prompt={false} />

## API Signature

### Methods

<PropertiesTable
  content={[
    {
      name: 'toAIStream',
      type: '(stream: ReadableStream<LangChainAIMessageChunk> | ReadableStream<string>, AIStreamCallbacksAndOptions) => AIStream',
      description: 'Converts LangChain output streams to AIStream.',
    },
  ]}
/>

## Examples

### Convert LangChain Expression Language Stream

```tsx filename="app/api/completion/route.ts" highlight={"14"}
import { ChatOpenAI } from '@langchain/openai';
import { LangChainAdapter, StreamingTextResponse } from 'ai';

export async function POST(req: Request) {
  const { prompt } = await req.json();

  const model = new ChatOpenAI({
    model: 'gpt-3.5-turbo-0125',
    temperature: 0,
  });

  const stream = await model.stream(prompt);

  const aiStream = LangChainAdapter.toAIStream(stream);

  return new StreamingTextResponse(aiStream);
}
```

### Convert StringOutputParser Stream

```tsx filename="app/api/completion/route.ts" highlight={"16"}
import { ChatOpenAI } from '@langchain/openai';
import { LangChainAdapter, StreamingTextResponse } from 'ai';
import { StringOutputParser } from '@langchain/core/output_parsers';

export async function POST(req: Request) {
  const { prompt } = await req.json();

  const model = new ChatOpenAI({
    model: 'gpt-3.5-turbo-0125',
    temperature: 0,
  });

  const parser = new StringOutputParser();
  const stream = await model.pipe(parser).stream(prompt);

  const aiStream = LangChainAdapter.toAIStream(stream);

  return new StreamingTextResponse(aiStream);
}
```
